* Paddy Carter, Nicolas Van de Sijpe, and Raphael Calel, 10-June-2020
* Define program elusivefirms to generate firm-level data


/* This do file is called by simfirm.do so there is no preamble here 
elusivefirms is a simplified version of elusive. 
We use gtools to speed up the code: https://github.com/mcaceresb/stata-gtools */


*************************************************************************
* PROGRAM	
*************************************************************************
/* Program key: */
/* (nC number of countries) (t years)  (dfbudg t=1 DFI budget as share of total opportunities) (mu mean project characteristics of three country types) (musd st. dev. for project characteristics of three country types) 
(ersd st. dev. of expected returns noise added to pc) (dfilo lower bound eligible dfi set) (dfihi upper bound dfi set) 
(psmin min. exp return for private sector) (nI num inv opportunities per country-year) (mechanism DFI inv selection mechanism: options are random, pclow)
*/


capture program drop elusivefirms
program define elusivefirms, rclass
	version 15.1
	syntax [, nC(integer 12) t(integer 1) /// 
	dfbudg(real 0.1) mu1(real 0) mu2(real 2) mu3(real 4) musd1(real 1) musd2(real 1) musd3(real 1) ersd(real 1) /// 
	dfilo(real 2) dfihi(real 1000) psmin(real 2) nI(real 500) mechanism(string)]

	
drop _all


set seed 7542629


* private sector budget (always exceeds available investment opportunities): 
local pb = `nI'*`nC'
* initial (year 1) value for the total DFI budget; defined as a fraction (dfbudg) of total number of annual investments:
local db = `dfbudg'*(`nI'*`nC')


/* Set up data: */	
* dataset:
local size = `nC'*`t'
set obs `size'
* number the countries:
gen country = ceil(_n/`t') 
* periods:
sort country	
by country: generate year = _n
xtset country year


**************************************************************************
* Generate weights to divide investments between multiple DFIs
**************************************************************************
* not relevant for firm-level data, so dropped.


**************************************************************************
* Country types
**************************************************************************
/* Initial country types: */
* 1/3 of each type
gen type = 1
replace type = 2 if country>`nC'*(1/3)
replace type = 3 if country>`nC'*(2/3)


/* No time dimension so no need for evolution of country types over time: */


**************************************************************************
* Budgets
**************************************************************************
/* T=1 so no need for time-varying DFI budget: */
gen int db = `db'


/* Private budget. */
gen double pb = `pb'


**************************************************************************
* Individual projects
**************************************************************************
/* Data for individual projects */
expand `nI'
sort country year
* create an index for individual projects
gen i = _n
order country year i


/* Project characteristics */
gen double pc = rnormal(`mu1',`musd1')
replace pc = rnormal(`mu2',`musd2') if type==2
replace pc = rnormal(`mu3',`musd3') if type==3
label var pc "Project characteristics"


/* Expected returns */
gen double er = pc + rnormal(0,`ersd')
label var er "Expected returns"


********************************************************************
* DFI investment selection mechanism
********************************************************************
if "`mechanism'" == "random" {
	/* DFI set of eligible projects is based on expected return thresholds. 
	Within this set, in each year the DFI randomly picks projects until its budget runs out. */


	/* Selection of investments by DFI sector as a whole. */
	* Set of investments DFIs would like to invest in:
	gen dfiset = inrange(er,`dfilo',`dfihi')
	

	/* In each year, DFI sector picks investments from this set of investment opportunities until the budget is exhausted */
	/* Put investable projects in random order and pick from the top until the budget is exhausted, 
	so that DFI sector randomly picks projects from its set of investment opportunities until it exhausts its budget. */
	gen double randnum = uniform()
	sort year randnum // randomly sort projects within each year
	* cumulative count of number of projects in each year DFI is interested in:
	by year: gen cum_dfi = sum(dfiset) 
	* DFI sector invests if project is in eligible set and still within the budget:
	generate byte dfi_inv = dfiset == 1 & cum_dfi <= db 
	
	
	* Divide up these investments between multiple DFIs -- not needed

	/* Selection of investment by private investors: */
	gen psset = 0
	* DFI sector has first dibs so private sector can only pick from investments not already taken by DFI:
	replace psset = 1 if er >= `psmin' & dfi_inv == 0
	* same trick to have private investors pick randomly from global set of projects they are interested in in each year
	sort year randnum
	by year: gen cum_ps = sum(psset)
	* private sector invests if the project is in the eligible set and still within the budget:
	generate byte ps_inv = psset == 1 & cum_ps <= pb
}
else if "`mechanism'" == "pclow" {
	/* DFI set of eligible projects is based on expected return thresholds.
	Within this set, in each year the DFI now first picks the projects with the worst project characteristics. */


	/* Selection of investments by DFI sector as a whole. */
	* Set of investments DFIs would like to invest in:
	gen dfiset = inrange(er,`dfilo',`dfihi')
	/* In each year, DFI sector picks investments from this set of investment opportunities until the budget is exhausted */
	/* Rank investment opportunities within each year by pc and pick lowest pc projects first, until budget is exhausted. */
	sort year pc
	* cumulative count of number of projects in each year DFI is interested in:
	by year: gen cum_dfi = sum(dfiset)
	* DFI invests if project is in eligible set and still within the budget:
	generate byte dfi_inv = dfiset == 1 & cum_dfi <= db


	* Divide up these investments between multiple DFIs -- not needed

	/* Selection of investment by private investors: */
	gen psset=0
	* DFI has first dibs so private sector can only pick from investments not already taken by DFI:
	replace psset = 1 if er >= `psmin' & dfi_inv == 0
	* have private investors pick randomly from global set of projects they are interested in in each year
	gen double randnum = uniform()
	sort year randnum
	by year: gen cum_ps = sum(psset)
	* private sector invests if the project is in the eligible set and still within the budget:
	generate byte ps_inv = psset == 1 & cum_ps <= pb 

}
else {
	di as error "Incorrect investment selection mechanism specified"
}
drop cum_dfi psset randnum cum_ps

sort country i

end 

